package com.gccloud.tgyfirst.controller.demo;

import com.gccloud.tgyfirst.demo.dto.CourseDTO;
import com.gccloud.tgyfirst.demo.entity.CourseEntity;
import com.gccloud.tgyfirst.service.demo.service.ICourseService;
import com.gccloud.tgyfirst.demo.vo.CourseVO;

import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.controller.SuperController;
import com.gccloud.starter.mybatis.page.PageVO;
import com.gccloud.starter.plugins.validator.ValidatorUtils;
import com.gccloud.starter.plugins.validator.group.Insert;
import com.gccloud.starter.plugins.validator.group.Update;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author qianxing
 * @version 1.0
 * @date 2021/5/17 15:52
 */
@RestController
@RequestMapping("/course")
/**
 * 定义在swagger中展示课程模块，并且它的接口排序为 20
 */
@Api(tags = "课程")
@ApiSort(value = 20)
public class CourseController extends SuperController {

    @Resource
    private ICourseService courseService;


    /**
     * 分页条件查询，获取课程列表
     *
     * @param searchDTO
     * @return
     */
    @GetMapping("/")
    @ApiOperation(value = "列表", position = 10, notes = "分页查询列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<CourseEntity>> getPage(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) SearchDTO searchDTO){
        PageVO<CourseEntity> page =  courseService.getPage(searchDTO);
        PageVO pageVO = BeanConvertUtils.convert(page, PageVO.class);
        pageVO.setList(page.getList());
        return success(pageVO);
    }

    /**
     * 分页查询已选课程
     * @param searchDTO
     * @return
     */
    @GetMapping("/myCourse")
    @ApiOperation(value = "列表", position = 10, notes = "我的课程列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<CourseEntity>> getMyCourse(@ApiParam(name = "查询", value = "传入查询的业务条件", required = true) SearchDTO searchDTO){
        PageVO<CourseEntity> page =  courseService.getMyCourse(searchDTO);
        PageVO pageVO = BeanConvertUtils.convert(page, PageVO.class);
        pageVO.setList(page.getList());
        return success(pageVO);
    }


    /**
     * 新增课程
     *
     * @param courseDTO
     * @return
     */
    @PostMapping
    @RequiresPermissions("course:add")
    @ApiOperation(value = "新增", position = 30, notes = "新增课程", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> add(@ApiParam(name = "新增课程JSON对象", value = "传入json格式", required = true) @RequestBody CourseDTO courseDTO){
        ValidatorUtils.validateEntity(courseDTO, Insert.class);
        CourseEntity courseEntity = BeanConvertUtils.convert(courseDTO, CourseEntity.class);
        courseService.add(courseEntity);
        return success(courseEntity.getId());
    }


    /**
     * 修改课程
     *
     * @param courseDTO
     * @return
     */
    @PutMapping
    @RequiresPermissions("course:update")
    @ApiOperation(value = "修改", position = 40, notes = "修改课程信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> update(@ApiParam(name = "修改课程JSON对象", value = "传入json格式", required = true) @RequestBody CourseDTO courseDTO) {
        ValidatorUtils.validateEntity(courseDTO, Update.class);
        CourseEntity courseEntity = BeanConvertUtils.convert(courseDTO, CourseEntity.class);
        courseService.update(courseEntity);
        return success();
    }

    /**
     * 根据id删除课程
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("course:delete")
    @ApiOperation(value = "删除", notes = "删除课程", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteById(@ApiParam(name = "课程ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        courseService.deleteByCourseId(id);
        return success();
    }

    /**
     * 根据id获取课程详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "详情", position = 20, notes = "课程详情", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/{id}")
    public R<CourseVO> getById(@ApiParam(name = "课程ID", value = "传入String格式", required = true) @PathVariable("id") String id) {
        CourseEntity courseEntity = courseService.getById(id);
        CourseVO courseVO = BeanConvertUtils.convert(courseEntity, CourseVO.class);
        return success(courseVO);
    }
}
